package interfata;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JPanel;
import functii.elementare.Constanta;
import arbori.ArboreFunctional;
import arbori.Nod;

//@SuppressWarnings("serial")
public class DesenArbore extends JPanel
{

	
	ArboreFunctional a;
	protected int raza=50;

/**	
 * initializarea graficului
 * 
 */
	protected void paintComponent(Graphics g)
	{
		super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);	
        g2.setColor(Color.lightGray);
		g2.fillRect(0, 0, 1000, 600);
		Inordine(g2,a.getRadacina(),0,1,1000,300,raza);
		g2.setColor(Color.lightGray);
		
		g.drawLine(0,50,480,50);
	}
	/**
	 * Afiseaza in graficul g nodul n la coordonatele furnizate cat si liniile dintre noduri
	 * @param g
	 * @param n
	 * @param nivel
	 * @param x1
	 * @param x2
	 * @param c1
	 * @param c2
	 */
	protected void paint(Graphics g,Nod n, int nivel, int x1, int x2, int c1, int c2) 
	{
		String sir=n.getClass().getName();
		int i=sir.lastIndexOf('.');
		sir=sir.substring(i+1);
		if(n.getClass().getName().equals("functii.elementare.Constanta"))
			sir=""+((Constanta)n).getCt();
		else sir=gasesteSemn(sir);		
		
		g.setColor(Color.darkGray);
		g.fillOval((x1+x2)/2-20, nivel*40+20, raza, raza);
		g.drawLine((x1+x2)/2,nivel*40+raza,c1,c2);
		g.setColor(Color.white);
		g.drawString(sir,(x1+x2)/2, nivel*40+raza);		
	}

	void Inordine(Graphics g,Nod n,int nivel,int x1,int x2,int c1,int c2)
	{
		if(n!=null)
			{
			 Inordine(g,n.getFStang(),nivel+1,x1,(x1+x2)/2,(x1+x2)/2,nivel*40+raza);
			 paint(g,n,nivel,x1,x2,c1,c2);
			 Inordine(g,n.getFDrept(),nivel+1,(x1+x2)/2,x2,(x1+x2)/2,nivel*40+raza);
			}
	}	
	
	public String gasesteSemn(String sir)
	{
		if(sir.equals("Plus"))
			return "+";
		else if(sir.equals("Minus"))
			return "-";
		else if(sir.equals("Ori"))
			return "*";
		else if(sir.equals("Impartit"))
			return "/";
		else if(sir.equals("Putere"))
			return "^";
		else if(sir.equals("Necunoscuta"))
			return "x";
		else if(sir.equals("Sin"))
			return "Sin";
		else if(sir.equals("Cos"))
			return "Cos";
		else return sir;
			
		
	}
	public void setArbore(Nod n)
	{
		this.a=new ArboreFunctional(n);
	}
	
}
